package org.example.order.controller;


import lombok.extern.slf4j.Slf4j;
import org.example.comment.utils.ThreadLocalUtil;
import org.example.constant.MessageConstant;
import org.example.dto.OrderDto;
import org.example.entity.Order;
import org.example.order.service.OrderService;
import org.example.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author fanfutian
 * @since 2025-04-23
 */

@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService  orderService;


    /**
     * 添加订单
     * @param orderDto
     * @return
     */
    @PostMapping("/addOrder")
    public Result addOrder(@RequestBody OrderDto orderDto){
        String userId = ThreadLocalUtil.get();
        if (userId == null){
            return Result.error(MessageConstant.LOGIN_ERROR);
        }
        log.info("用户id："+userId+"商品信息："+orderDto);
        orderService.addOrder(userId, orderDto);
        return Result.success();
    }


    /**
     * 根据订单id查询订单
     * @param orderId
     * @return
     */
    @GetMapping("/getOrderByOrderId/{orderId}")
    public Result<Order> getOrderById(@PathVariable("orderId") Long orderId){
        log.info("根据订单id：{} 查询订单数据",orderId);
        Order order = orderService.getOrderByOrderId(orderId);
        return Result.success(order);
    }



    /**
     * 根据用户id查询订单
     * @param userId
     * @return
     */
    @GetMapping("/getOrderByUserId/{userId}")
    public Result<List<Order>> getOrderByUserId(@PathVariable("userId") Long userId){
        log.info("根据用户id：{} 查询订单数据",userId);
        List<Order> orders = orderService.getOrderByUserId(userId);
        return Result.success(orders);
    }

}
